Neral Networks By Sara Jones

Libraries needed


In [51]:
import numpy as np
import math
import random
import string

In [52]:
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.html.widgets import interact

In [53]:
from sklearn.datasets import load_digits
digits = load_digits()
print(digits.data.shape)


(1797, 64)

Refrance Information


In [54]:
def show_digit(i):
    plt.matshow(digits.images[i]);

In [55]:
interact(show_digit, i=(0,100));


Start of Neral Network


In [56]:
"""Neron for for detrminig what is put in and the weights of certin aspects of the input"""
class Neuron:
        def __init__(self, n_inputs ):
            self.n_inputs = n_inputs
            self.set_weights( [random.uniform(0,1) for x in range(0,n_inputs+1)] )
            
        def sum(self, inputs ):
            return sum(val*self.weights[i] for i,val in enumerate(inputs))

        def set_weights(self, weights ):
            self.weights = weights
        
        def __str__(self):
            return ( str(self.weights[:-1]),str(self.weights[-1]) )

In [57]:
class NeuronLayer:
        def __init__(self, n_neurons, n_inputs):
            self.n_neurons = n_neurons
            self.neurons = [Neuron( n_inputs ) for _ in range(0,self.n_neurons)]

        def __str__(self):
            return 'Layer:\n\t'+'\n\t'.join([str(neuron) for neuron in self.neurons])+''

In [58]:
"""Training set"""
def learn(self, X, y, learning_rate=0.2, epochs=10000):
    X = np.atleast_2d(X)
    temp = np.ones([X.shape[0], X.shape[1]+1])
    temp[:, 0:-1] = X  
    X = temp
    y = np.array(y)

    for i in range(epochs):
        k = np.random.randint(X.shape[0])
        a = [X[i]]

        for j in range(len(self.weights)):
            a.append(self.activation(np.dot(a[j], self.weights[j])))
        error = y[i] - a[-1]
        deltas = [error * self.activation_deriv(a[-1])]

        for i in range(len(a) - 2, 0, -1):
            deltas.append(deltas[-1].dot(self.weights[i].T)*self.activation_deriv(a[i]))
        deltas.reverse()
        
        for i in range(len(self.weights)):
            layer = np.atleast_2d(a[i])
            delta = np.atleast_2d(deltas[i])
            self.weights[i] += learning_rate * layer.T.dot(delta)

In [59]:
def guess(self, t):
    t = np.array(t)
    temp = np.ones(t.shape[0]+1)
    temp[0:-1] =t
    a = temp
    for i in range(0, len(self.weights)):
        k = self.activation(np.dot(k, self.weights[i]))
        return k

In [ ]:


In [ ]:


In [ ]:


In [ ]: